package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.extend.WorkFinishOrderTemplateFieldValExtend;
import com.zb.mapper.ZjWorkFinishOrderTemplateFieldValMapper;
import com.zb.service.ZjWorkFinishOrderTemplateFieldValOptionService;
import com.zb.service.ZjWorkFinishOrderTemplateFieldValService;
import com.zb.service.ZjWorkStatementTemplateFieldService;
import com.zb.service.ZjWorkStatementTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 工单结单模板保存字段值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-30
 */
@Service
public class ZjWorkFinishOrderTemplateFieldValServiceImpl extends ServiceImpl<ZjWorkFinishOrderTemplateFieldValMapper, ZjWorkFinishOrderTemplateFieldVal> implements ZjWorkFinishOrderTemplateFieldValService {
    @Resource
    private ZjWorkFinishOrderTemplateFieldValMapper workFinishOrderTemplateFieldValMapper;
    @Resource
    private ZjWorkFinishOrderTemplateFieldValOptionService workFinishOrderTemplateFieldValOptionService;
    @Resource
    private ZjWorkStatementTemplateFieldService workStatementTemplateFieldService;
    @Resource
    private ZjWorkStatementTemplateService workStatementTemplateService;

    // 根据工单结单模板字段表ID获取字段内容
    @Override
    public List<WorkFinishOrderTemplateFieldValExtend> getByWorkStatementTemplateIds(List<Long> workStatementTemplateIds) {
        if (CollectionUtil.isEmpty(workStatementTemplateIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkFinishOrderTemplateFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_finish_order_template_id", "work_statement_template_field_id", "type")
                .in("work_finish_order_template_id", workStatementTemplateIds);
        List<ZjWorkFinishOrderTemplateFieldVal> fieldValList = this.list(wrapper);
        if (CollectionUtil.isEmpty(fieldValList)) {
            return new ArrayList<>();
        }
        List<ZjWorkFinishOrderTemplateFieldValOption> valOptions = workFinishOrderTemplateFieldValOptionService.getByWorkFinishOrderTemplateFieldValIds(fieldValList.stream().map(ZjWorkFinishOrderTemplateFieldVal::getId).toList());
        QueryWrapper<ZjWorkStatementTemplateField> statementTemplateFieldQueryWrapper = new QueryWrapper<>();
        statementTemplateFieldQueryWrapper.select("id", "name")
                .in("id", fieldValList.stream().map(ZjWorkFinishOrderTemplateFieldVal::getWorkStatementTemplateFieldId).distinct().toList());
        List<ZjWorkStatementTemplateField> templateFieldList = workStatementTemplateFieldService.list(statementTemplateFieldQueryWrapper);
        return fieldValList.stream().map(field -> {
            WorkFinishOrderTemplateFieldValExtend extend = new WorkFinishOrderTemplateFieldValExtend();
            BeanUtil.copyProperties(field, extend);
            templateFieldList.stream().filter(f -> Objects.equals(f.getId(), field.getWorkStatementTemplateFieldId())).findFirst()
                    .map(ZjWorkStatementTemplateField::getName).ifPresent(extend::setName);
            extend.setVals(valOptions.stream().filter(f -> Objects.equals(f.getWorkFinishOrderTemplateFieldValId(), field.getId())).map(val -> {
                return new FieldValOptionVo(val.getId(), val.getOptValue());
            }).toList());
            return extend;
        }).toList();
    }

    // 根据工单ID获取结单字段信息
    @Override
    public List<WorkFinishOrderTemplateFieldValExtend> getByWorkIds(List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkFinishOrderTemplateFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "work_finish_order_template_id", "work_statement_template_field_id")
                .in("type", 1,2,3,4,8)
                .in("work_id", workIds);
        List<ZjWorkFinishOrderTemplateFieldVal> fieldValList = this.list(wrapper);
        if (CollectionUtil.isEmpty(fieldValList)) {
            return new ArrayList<>();
        }
        List<WorkFinishOrderTemplateFieldValExtend> templateFieldValExtends = fieldValList.stream().map(val -> {
            WorkFinishOrderTemplateFieldValExtend extend = new WorkFinishOrderTemplateFieldValExtend();
            BeanUtil.copyProperties(val, extend);
            return extend;
        }).toList();
        // 查询模板
        QueryWrapper<ZjWorkStatementTemplate> templateQueryWrapper = new QueryWrapper<>();
        templateQueryWrapper.select("id", "name")
                .in("id", fieldValList.stream().map(ZjWorkFinishOrderTemplateFieldVal::getWorkFinishOrderTemplateId).distinct().toList());
        List<ZjWorkStatementTemplate> templateList = workStatementTemplateService.list(templateQueryWrapper);
        // 查询字段名称
        QueryWrapper<ZjWorkStatementTemplateField> templateFieldQueryWrapper = new QueryWrapper<>();
        templateFieldQueryWrapper.select("id", "name")
                .in("id", fieldValList.stream().map(ZjWorkFinishOrderTemplateFieldVal::getWorkStatementTemplateFieldId).distinct().toList());
        List<ZjWorkStatementTemplateField> templateFieldList = workStatementTemplateFieldService.list(templateFieldQueryWrapper);
        // 查询字段内容
        QueryWrapper<ZjWorkFinishOrderTemplateFieldValOption> optionQueryWrapper = new QueryWrapper<>();
        optionQueryWrapper.select("work_finish_order_template_field_val_id", "opt_value")
                .in("work_finish_order_template_field_val_id", fieldValList.stream().map(ZjWorkFinishOrderTemplateFieldVal::getId).toList());
        List<ZjWorkFinishOrderTemplateFieldValOption> optionList = workFinishOrderTemplateFieldValOptionService.list(optionQueryWrapper);
        templateFieldValExtends.stream().forEach(valExtend -> {
            valExtend.setTemplateName(templateList.stream().filter(f -> Objects.equals(f.getId(), valExtend.getWorkFinishOrderTemplateId())).findFirst().map(ZjWorkStatementTemplate::getName).orElse(""));
            valExtend.setName(templateFieldList.stream().filter(f -> Objects.equals(f.getId(), valExtend.getWorkStatementTemplateFieldId())).findFirst().map(ZjWorkStatementTemplateField::getName).orElse(""));
            valExtend.setVals(optionList.stream().filter(f -> Objects.equals(f.getWorkFinishOrderTemplateFieldValId(), valExtend.getId())).map(opt -> {
                return new FieldValOptionVo(opt.getId(), opt.getOptValue());
            }).toList());
        });
        return templateFieldValExtends;
    }

    // 根据工单ID与固定的结单模板ID获取结单自定义字段内容
    @Override
    public List<WorkFinishOrderTemplateFieldValExtend> getPushFieldByWorkIdAndWorkStatementTemplateId(Long workId, Long finishTemplateId) {
        List<WorkFinishOrderTemplateFieldValExtend> fieldValExtends = workFinishOrderTemplateFieldValMapper.getPushFieldByWorkIdAndWorkStatementTemplateId(workId, finishTemplateId);
        if (CollectionUtil.isEmpty(fieldValExtends)) {
            return new ArrayList<>();
        }
        // 查询字段内容
        QueryWrapper<ZjWorkFinishOrderTemplateFieldValOption> optionQueryWrapper = new QueryWrapper<>();
        optionQueryWrapper.select("work_finish_order_template_field_val_id", "opt_value")
                .in("work_finish_order_template_field_val_id", fieldValExtends.stream().map(WorkFinishOrderTemplateFieldValExtend::getId).toList());
        List<ZjWorkFinishOrderTemplateFieldValOption> optionList = workFinishOrderTemplateFieldValOptionService.list(optionQueryWrapper);
        fieldValExtends.stream().forEach(valExtend -> {
            valExtend.setVals(optionList.stream().filter(f -> Objects.equals(f.getWorkFinishOrderTemplateFieldValId(), valExtend.getId())).map(opt -> {
                return new FieldValOptionVo(opt.getId(), opt.getOptValue());
            }).toList());
        });
        return fieldValExtends;
    }
}
